//
//  OffsetListViewController.m
//  Supermarket
//
//  Created by qskj-ios on 2018/12/10.
//  Copyright © 2018年 com.qiansheng. All rights reserved.
//

#import "OffsetListViewController.h"
#import "OffsetListCell.h"
#import "QueryViewController.h"

@interface OffsetListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation OffsetListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, TopHeight+40, 0);
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([OffsetListCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([OffsetListCell class])];
    
    WEAKSELF
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf.dataArray removeAllObjects];
        [weakSelf getDataSource];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryNotification) name:@"QuerySuccessNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryNotification) name:@"QuerySingOutNotification" object:nil];
}

- (void)queryNotification {
    [self.tableView.mj_header beginRefreshing];
}

- (void)getDataSource {
    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
    [parameters setObject:UserInfo.id forKey:@"user_id"];
    
    WEAKSELF
    [NetworkHelper apiPost:@"manager/user_offset" parameters:parameters completion:^(id info, NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        if (!error) {
            if ([[info objectForKey:@"sta"] integerValue] == 1) {
                [weakSelf.dataArray addObjectsFromArray:[OffsetModel mj_objectArrayWithKeyValuesArray:[info objectForKey:@"response"]]];
            }
            [SupermarketUtil setNullDataView:weakSelf.view withData:weakSelf.dataArray];
            [weakSelf.tableView reloadData];
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OffsetListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([OffsetListCell class]) forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    OffsetModel *model = [self.dataArray objectAtIndex:indexPath.row];
    [cell setContentWithOffset:model];
    
    WEAKSELF
    cell.exchangeButtonBlock = ^(UITableViewCell * _Nonnull cell) {
        NSIndexPath *indexPath = [tableView indexPathForCell:cell];
        [weakSelf didClickedExchangeButton:indexPath];
    };
    
    return cell;
}

- (void)didClickedExchangeButton:(NSIndexPath *)indexPath {
    OffsetModel *model = [self.dataArray objectAtIndex:indexPath.row];
    
    if (![BasePresentationController isAnimating] && !self.hasBasePresentedController && model.sta == 0) {
        QueryViewController *dvc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"QueryViewController"];
        dvc.type = 0;
        dvc.offset_id = model.id;
        BasePresentationController *presentationController = [[BasePresentationController alloc] initWithContentViewController:dvc];
        presentationController.contentViewControllerTransitionStyle = MZFormSheetPresentationTransitionStyleBounce;
        presentationController.contentViewSize = CGSizeMake(283, 340);
        presentationController.shouldCenterVertically = YES;
        [presentationController presentFromViewController:self animated:YES completion:nil];
        WEAKSELF
        dvc.querySuccessBlock = ^{
            model.sta = 1;
            [weakSelf.tableView reloadData];
        };
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
